|
|
|
|
|
|
|
Example program Reflect, shown in Figure 7.20, uses this code to reflect an image across a line with equation Y = M * X + B. Enter the values for M and B on the form and click the Reflect button. Keep in mind that the picture's coordinate system starts with (0, 0) in the upper left corner so the line may seem upside down. Setting B = 0 makes the line pass through the origin. |
|
|
|
|
|
|
|
|
Shape-Distorting Transformation |
|
|
|
|
|
|
|
|
So far, the geometric transformations described in this chapter have left the image's shape more or less unchanged. If you enlarge or shrink an image more in one direction than another, you can stretch or flatten an image, but straight lines are still straight lines in the result. |
|
|
|
|
|
|
|
|
There is no reason you cannot implement transformations that warp an image in stranger ways. The method is the same as before: Map the output pixels to the input pixel locations and use bilinear interpolation to fill in any gaps. |
|
|
|
|
|
|
|
|
For example, consider the transformation functions below. |
|
|
|
 |
|
|
|
|
x_out = x_in
y_out = y_in + 20 * (Sin(x_in / 30) + 1) |
|
|
|
|
|
|
|
|
It is easy to solve these equations for x_in and y_in to find the inverse transformation. |
|
|
|
 |
|
|
|
|
x_in = x_out
y_in = y_out - 20 * (Sin(x_in / 30) + 1)
= y_out - 20 * (Sin(x_out / 30) + 1) |
|
|
|
|
|
|
|
|
Using these equations, you can build a MapPixel subroutine to map output pixels to input pixel locations. Example program Warp, shown in Figure 7.21, uses this technique to demonstrate several different shape-distorting transformations. |
|
|
|
|
|
|
|
|
Example programs Fish and Pinch use the same technique with different user interfaces. Load a picture and click on it. Program Fish enlarges the position you click. Program Pinch shrinks the position you click. |
|
|
|
|
|
|
|
|
Figure 7.20
Program Reflect reflects an image across an arbitrary line. |
|
|
|
|
|